OFFLINE = 0
ONLINE = 1
AWAY = 2

class Member:
    def __init__(self, id):
        self._id = id
        self._name = id
        self._stamp = None
        self._isAdmin = False
        self._isBanned = False
        self._state = OFFLINE

    def setName(self, newName):
        self._name = newName

    def setStamp(self, newStamp):
        if newStamp == 'NONE':
            self._stamp = None
        else:
            self._stamp = newStamp

    def setAdmin(self, admin):
        self._isAdmin = admin

    def setBanned(self, banned):
        self._isBanned = banned

    def setState(self, newState):
        self._state = newState

    def isBanned(self):
        return self._isBanned
    
    def displayName(self):
        if self._stamp != None:
            ret = '*[%s]* %s' % (self._stamp, self._name)
        else:
            ret = '*%s*' % self._name
        return ret

    def simpleInfo(self):
        ret = '%s=%s' % (self._id, self.displayName())
        if (self._isAdmin):
            ret = '@%s' % ret

        if (self._isBanned):
            ret = '#%s' % ret

        if (self._state == OFFLINE):
            ret = '(%s)' % ret
        elif (self._state == AWAY):
            ret = '-%s' % ret

        return ret

    def detailInfo(self):
        ret = []
        ret.append('ID=%s' % self._id)
        ret.append('Name=%s' % self.displayName())
        if (self._isAdmin):
            ret.append('Admin=True')
        else:
            ret.append('Admin=False')

        if (self._isBanned):
            ret.append('Banned=True')
        else:
            ret.append('Banned=False')

        if (self._state == OFFLINE):
            ret.append('Status=OFFLINE')
        elif (self._state == AWAY):
            ret.append('Status=AWAY')
        else:
            ret.append('Status=ONLINE')

        return '\n'.join(ret)


if __name__ == '__main__':
    import unittest

    class TestMember(unittest.TestCase):
        def testInitialValue(self):
            m = Member('ch.linghu')
            self.assertEqual(m._id, 'ch.linghu')
            self.assertEqual(m._name, 'ch.linghu')
            self.assertEqual(m._stamp, None)
            self.assertEqual(m._isAdmin, False)
            self.assertEqual(m._state, OFFLINE)

        def testDisplayName(self):
            m = Member('raptor.zh')
            self.assertEqual(m.displayName(), '*raptor.zh*')

            #Change name
            m.setName('Raptor')
            self.assertEqual(m.displayName(), '*Raptor*')
        
            #Use stamp
            m.setStamp('King')
            self.assertEqual(m.displayName(), '*[King]* Raptor')

            #Use special charactor in stamp
            m.setStamp('money][sell')
            self.assertEqual(m.displayName(), '*[money][sell]* Raptor')

            #Change name again
            m.setName('MQ')
            self.assertEqual(m.displayName(), '*[money][sell]* MQ')

            #Remove the stamp
            m.setStamp('NONE')
            self.assertEqual(m.displayName(), '*MQ*')

        def testSimpleInfo(self):
            m = Member('ch.linghu')
            self.assertEqual(m.simpleInfo(), '(ch.linghu=*ch.linghu*)')

            #make as admin
            m.setAdmin(True)
            self.assertEqual(m.simpleInfo(), '(@ch.linghu=*ch.linghu*)')

            #make online
            m.setState(ONLINE)
            self.assertEqual(m.simpleInfo(), '@ch.linghu=*ch.linghu*')

            #banned
            m.setBanned(True)
            self.assertEqual(m.simpleInfo(), '#@ch.linghu=*ch.linghu*')

            #change name
            m.setName('topcat')
            self.assertEqual(m.simpleInfo(), '#@ch.linghu=*topcat*')

            #make a stamp
            m.setStamp('admin')
            self.assertEqual(m.simpleInfo(), '#@ch.linghu=*[admin]* topcat')

            #make away
            m.setState(AWAY)
            self.assertEqual(m.simpleInfo(), '-#@ch.linghu=*[admin]* topcat')

            #remove admin
            m.setAdmin(False)
            self.assertEqual(m.simpleInfo(), '-#ch.linghu=*[admin]* topcat')

            #unbanned
            m.setBanned(False)
            self.assertEqual(m.simpleInfo(), '-ch.linghu=*[admin]* topcat')

        def testDetailInfo(self):
            m = Member('ch.linghu')
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*ch.linghu*
Admin=False
Banned=False
Status=OFFLINE''')

            #make as admin
            m.setAdmin(True)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*ch.linghu*
Admin=True
Banned=False
Status=OFFLINE''')

            #make online
            m.setState(ONLINE)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*ch.linghu*
Admin=True
Banned=False
Status=ONLINE''')

            #banned
            m.setBanned(True)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*ch.linghu*
Admin=True
Banned=True
Status=ONLINE''')

            #change name
            m.setName('topcat')
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*topcat*
Admin=True
Banned=True
Status=ONLINE''')

            #make a stamp
            m.setStamp('admin')
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*[admin]* topcat
Admin=True
Banned=True
Status=ONLINE''')

            #make away
            m.setState(AWAY)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*[admin]* topcat
Admin=True
Banned=True
Status=AWAY''')

            #remove admin
            m.setAdmin(False)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*[admin]* topcat
Admin=False
Banned=True
Status=AWAY''')

            #unbanned
            m.setBanned(False)
            self.assertEqual(m.detailInfo(), \
'''ID=ch.linghu
Name=*[admin]* topcat
Admin=False
Banned=False
Status=AWAY''')


    unittest.main()
             
